package com.guigu.pd.base.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.serviceapi.pojo.TransportLine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.TransportLineEntity;
import com.guigu.pd.base.service.TransportLineService;
import com.guigu.common.utils.R;



/**
 * 线路表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@Api(tags = "线路管理")
@RestController
@RequestMapping("base/transportline")
public class TransportLineController {
    @Autowired
    private TransportLineService transportLineService;


    @Autowired
    CustomIdGenerator customIdGenerator;


    /**
     * 添加线路
     *
     * @return 线路信息
     */
    @ApiOperation("添加线路")
    @GetMapping("/saveTransportLine")
    public R saveTransportLine(@RequestParam Map<String, Object> params) {
        TransportLineEntity
                transportLine = JSON.parseObject(JSON.toJSONString(params),
                TransportLineEntity.class);
        transportLine.setId(customIdGenerator.nextId(transportLine)+"");

        boolean save = transportLineService.save(transportLine);

        if(save){
            return R.ok("添加成功");
        }

        return R.ok("添加失败");
    }

    /**
     * 根据id获取线路详情
     *
     * @param id 线路id
     * @return 线路详情
     */
    @ApiOperation("根据id获取线路详情")
    @GetMapping("/fineTransportLineById/{id}")
    public TransportLineEntity fineTransportLineById(@PathVariable(name = "id") String id) {
        TransportLineEntity transportLine = transportLineService.getById(id);

        return transportLine;

    }

    /**
     * 更新线路信息
     * @return 线路信息
     */
    @ApiOperation("更新线路信息")
    @PutMapping("/updateTransportLinebyid")
    public R updateTransportLinebyid(@RequestParam Map<String, Object> params) {
        TransportLineEntity
                transportLine = JSON.parseObject(JSON.toJSONString(params),
                TransportLineEntity.class);
        boolean b = transportLineService.updateById(transportLine);

        if(b){
            return R.ok("修改成功");
        }

        return R.ok("修改失败");
    }

    /**
     * 删除线路
     *
     * @param id 线路id
     * @return 返回信息
     */
    @ApiOperation("删除线路")
    @PutMapping("/delectTransportLinebyid/{id}")
    public R delectTransportLinebyid(@PathVariable(name = "id") String id) {
        boolean b = transportLineService.removeById(id);

        if(b){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");
    }


    /**
     * 获取线路分页信息和该线路下数车次
     * @return 线路分页信息
     */
    @ApiOperation("获取线路分页信息和该线路下数车次")
    @GetMapping("/querytransportLinefy")
    public R  querytransportLinefy(@RequestParam Map<String, Object> params) throws ParseException {
        PageVo<TransportLineEntity> list = transportLineService.querytransportLinefy(params);
        return R.ok().put("line",list);
    }



    //根据起点网点id去 base库 路线表去查询所有起点为起点网点id 查询数据 （包括直达 或者要中转）
    @GetMapping("/getTransportLinelistbystartAgencyId/{startAgencyId}/{getpidbyendid}")
   public TransportLine getTransportLinelistbystartAgencyId(@PathVariable("startAgencyId") String startAgencyId,@PathVariable("getpidbyendid") String getpidbyendid){

        LambdaQueryWrapper<TransportLineEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TransportLineEntity::getStartAgencyId,startAgencyId).eq(TransportLineEntity::getEndAgencyId,getpidbyendid);
        TransportLineEntity one = transportLineService.getOne(lambdaQueryWrapper);

        TransportLine transportLine = new TransportLine();

        BeanUtils.copyProperties(transportLine,one);

        return transportLine;
    }



}
